import calendar
from configobj import ConfigObj

class DayStructure:

    day = 0         #默认0
    content = ""    #默认空, 用\n换行

    #默认初始化
    def __init__(self):
        pass

    #可视化
    def __str__(self):
        return ("day = %d, content = %s" % (self.day, self.content))

    def setDay(self, day):
        self.day = day

    def setContent(self, content):
        self.content = content.replace('\\n', '\n')

def generateOneMonth(year, month, configObj):
    calendar.setfirstweekday(firstweekday=6)
    OneMonthDay = calendar.monthcalendar(year, month)
    
    #以下段无需assert, 因为calendar不会出错: SyntaxWarning: assertion is always true, perhaps remove parentheses
    #assert(
    #    len(OneMonthDay) > 0, 
    #    "the return value of calendar.monthcalendar(%d, %d) has zero lines, please check" % (year, month)
    #    )   #确保非0行
    #assert(
    #    len(OneMonthDay[0]) > 0,
    #    "the return value of calendar.monthcalendar(%d, %d) has zero columns, please check" % (year, month)
    #    )   #确保非0列

    OneMonthDayDetail = [
        [DayStructure() for col in range(len(OneMonthDay[0]))] for row in range(len(OneMonthDay))
        ]
    
    for rowIndex, rowContent in enumerate(OneMonthDay):
        for colIndex, day in enumerate(rowContent):
            if day != 0:
                OneMonthDayDetail[rowIndex][colIndex].setDay(day)
                tempContent = ""
                try:
                    tempContent = configObj[str(year)][str(month)][str(day)]
                except KeyError:
                    tempContent = ""
                OneMonthDayDetail[rowIndex][colIndex].setContent(tempContent)
    
    return OneMonthDayDetail

#测试
if __name__ == "__main__":
    OneMonthDayDetailList = generateOneMonth(2020, 9, ConfigObj('data.cfg'))
    for rowContent in OneMonthDayDetailList:
        for colContent in rowContent:
            print(colContent)